﻿using Infrastructure.Crosscutting.Adapter;
using Infrastructure.Crosscutting.Validator;
using Microsoft.Practices.Unity;
using System.Crosscutting.Validator;
using Ueyes.Common.Application;
using Ueyes.Common.Infrastructure.Data;
using WinFormsMvp.Binder;
using WinFormsMvp.Unity;

namespace UeyesAIS
{
    public static class Container
    {
        #region Properties

        static IUnityContainer _currentContainer;

        /// <summary>
        /// Get the current configured container
        /// </summary>
        /// <returns>Configured container</returns>
        public static IUnityContainer Current
        {
            get
            {
                return _currentContainer;
            }
        }

        #endregion

        #region Constructor

        static Container()
        {
            ConfigureContainer();

            ConfigureFactories();
        }

        #endregion
        static void ConfigureContainer()
        {
            #region 服务注入
            _currentContainer = new UnityContainer();
            //var userService =new MockUserService();
            var userService = new UserService(new UserRepository(new CommonUnitOfWorkSQLite()));
            _currentContainer.RegisterInstance<IUserService>(userService);
            PresenterBinder.Factory = new UnityPresenterFactory(_currentContainer);
            #endregion
        }
        static void ConfigureFactories()
        {
            #region 初始化数据库
            //System.Data.Entity.Database.SetInitializer(new DropCreateDatabaseIfModelChanges<CommonUnitOfWork>());
            #endregion
            #region DTO映射
            System.Crosscutting.Adapter.TypeAdapterFactory.SetCurrent(new AutomapperTypeAdapterFactory());
            #endregion
            ValidatorFactory.SetCurrent(new DataAnnotationsValidatorFactory());
        }
    }
}
